package com.ocm.commerceinteraction.util.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ocm.commerceinteraction.R;


/**
 * Description:自定义弹出框（竖屏）
 * 
 * @author curry
 * @Date 2014-9-23
 * @Copyright:2012-09-23
 */
public class MyAlertVDialog {
	Context context;
	AlertDialog ad;
	TextView titleView;
	TextView messageView;
	LinearLayout buttonLayout, ll_style, ll_title;

	Button btn_one, btn_two;

	// int height = 0;
	// int width = 0;

	public MyAlertVDialog(Context _context) {
		this.context = (Context) _context;

		// 获取屏幕宽高
		// DisplayMetrics dm = new DisplayMetrics();
		// context.getWindowManager().getDefaultDisplay().getMetrics(dm);
		// int width1 = dm.widthPixels;
		// int height1 = dm.heightPixels;
		ad = new AlertDialog.Builder(context).create();
		ad.setCanceledOnTouchOutside(false);
		ad.setOnKeyListener(new DialogInterface.OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					return true;
				} else {
					return false; // 默认返回 false
				}
			}
		});
		ad.show();
		// 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
		Window window = ad.getWindow();
		window.setContentView(R.layout.view_myalertdialog_v);

		titleView = (TextView) window.findViewById(R.id.title);
		messageView = (TextView) window.findViewById(R.id.message);
		buttonLayout = (LinearLayout) window.findViewById(R.id.buttonLayout);
		ll_style = (LinearLayout) window.findViewById(R.id.ll_style);
		btn_one = (Button) window.findViewById(R.id.btn_one);
		btn_two = (Button) window.findViewById(R.id.btn_two);
		ll_title = (LinearLayout) window.findViewById(R.id.ll_title);
		ll_title.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				dismiss();
			}
		});
		// //获取layout参数
		// LayoutParams rlParams = (LayoutParams) ll_style.getLayoutParams();
		// //设置高度
		// rlParams.height = height;
		// rlParams.width = width;
		// ll_style.setLayoutParams(rlParams);
	}
	/**
	 * 
	 * @param _context 
	 * @param isSelect 是否需要取消按钮 
	 */
	public MyAlertVDialog(Context _context, boolean isSelect) {
		this.context = (Context) _context;

		// 获取屏幕宽高
		// DisplayMetrics dm = new DisplayMetrics();
		// context.getWindowManager().getDefaultDisplay().getMetrics(dm);
		// int width1 = dm.widthPixels;
		// int height1 = dm.heightPixels;
		ad = new AlertDialog.Builder(context).create();
		ad.setCanceledOnTouchOutside(false);
		ad.setOnKeyListener(new DialogInterface.OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					return true;
				} else {
					return false; // 默认返回 false
				}
			}
		});
		ad.show();
		// 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
		Window window = ad.getWindow();
		window.setContentView(R.layout.view_myalertdialog_v);

		titleView = (TextView) window.findViewById(R.id.title);
		messageView = (TextView) window.findViewById(R.id.message);
		buttonLayout = (LinearLayout) window.findViewById(R.id.buttonLayout);
		ll_style = (LinearLayout) window.findViewById(R.id.ll_style);
		btn_one = (Button) window.findViewById(R.id.btn_one);
		btn_two = (Button) window.findViewById(R.id.btn_two);
		if (!isSelect) {
			btn_two.setVisibility(View.GONE);
		}
		ll_title = (LinearLayout) window.findViewById(R.id.ll_title);
		ll_title.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				dismiss();
			}
		});
		// //获取layout参数
		// LayoutParams rlParams = (LayoutParams) ll_style.getLayoutParams();
		// //设置高度
		// rlParams.height = height;
		// rlParams.width = width;
		// ll_style.setLayoutParams(rlParams);
	}

	public void setTitle(int resId) {
		titleView.setText(resId);
	}

	public void setTitle(String title) {
		titleView.setText(title);
	}

	public void setMessage(int resId) {
		messageView.setText(resId);
	}

	public void setMessage(String message) {
		messageView.setText(message);
	}

	// public void setMinusHeightOrWeith(int _width,int _height)
	// {
	// height = height-_height;
	// width = width-_width;
	// //获取layout参数
	// LayoutParams rlParams = (LayoutParams) ll_style.getLayoutParams();
	// //设置高度
	// rlParams.height = height;
	// rlParams.width = width;
	// ll_style.setLayoutParams(rlParams);
	// }

	/**
	 * 设置按钮
	 * 
	 * @param text
	 * @param listener
	 */
	public void setPositiveButton(String text,
			final OnClickListener listener) {
		btn_two.setText(text);
		btn_two.setOnClickListener(listener);
	}

	/**
	 * 设置按钮
	 * 
	 * @param text
	 * @param listener
	 */
	public void setNegativeButton(String text,
			final OnClickListener listener) {
		btn_one.setText(text);
		btn_one.setOnClickListener(listener);
	}

	/**
	 * 关闭对话框
	 */
	public void dismiss() {
		ad.dismiss();
	}
}
